﻿using System;
using System.IO;
using System.Web.Mvc;
using System.Web.Routing;

namespace WebStickyNotes
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = "" }
            );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);
        }

        protected void Application_Error(Object sender, System.EventArgs e)
        {
            //HttpContext context = HttpContext.Current;
            var exception = Context.Server.GetLastError();

            LogError(exception); //Your own logging function...
        }

        private void LogError(Exception ex)
        {

            // Compose a string that consists of three lines.
            var lines = string.Format("{0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace);

            // Write the string to a file.
            using (var file = new StreamWriter("log.txt"))
            {
                file.WriteLine(lines);
                file.Close();
            }
        }
    }
}